package com.lambkit.module.cms.web.api.controller;

import com.jfinal.plugin.activerecord.Page;
import com.lambkit.module.cms.common.CmsResult;
import com.lambkit.module.cms.rpc.model.CmsAccess;
import com.lambkit.module.cms.rpc.model.CmsArticle;
import com.lambkit.module.upms.UpmsManager;
import com.lambkit.module.upms.rpc.model.UpmsUser;
import com.lambkit.web.controller.LambkitController;

/**
 * 笔记
 * @author yangyong
 *
 */
public class NoteApiController extends LambkitController {

	/**
     * 笔记
     */
    public void index() {
    	String action = getPara(0);
    	if("page".equalsIgnoreCase(action)) {
    		//笔记列表
    		int num = getParaToInt(1, 1);
    		int row = getParaToInt(2, 10);
    		UpmsUser user = getAttr("upmsUser") ==null ? UpmsManager.me().currentUser() : getAttr("upmsUser");
    		Page<CmsArticle> notes = CmsArticle.service().paginate(num, row, CmsArticle.sql().andUserIdEqualTo(user.getUserId()).example());
    		renderJson(CmsResult.success(notes));
    	} else if("category".equalsIgnoreCase(action)) {
    		//分类笔记列表
    		Long categoryId = getParaToLong(1);
    		int num = getParaToInt(2, 1);
    		int row = getParaToInt(3, 10);
    		Page<CmsArticle> notes = CmsArticle.service().paginateByCategoryIdInNormal(num, row, categoryId, "ctime desc");
    		renderJson(CmsResult.success(notes));
    	} else {
    		//笔记记录
    		Long articleId = getParaToLong(0);
    		UpmsUser user = getAttr("upmsUser") ==null ? UpmsManager.me().currentUser() : getAttr("upmsUser");
    		if(user!=null && !user.getUserId().equals(1L)) {
    			CmsAccess.service().addArticleAccess(articleId, user.getUserId(), "read");
    		}
        	renderJson(CmsResult.success(CmsArticle.service().findById(articleId)));
    	}
    }
}
